package com.kalle.workflow.engine;

import com.kalle.workflow.util.IdGenerator;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.IOException;

public class WorkflowEngineConfiguration {

    private DataSource dataSource;

    private IdGenerator idGenerator;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setIdGenerator(IdGenerator idGenerator) {
        this.idGenerator = idGenerator;
    }

    public void setJndiDataSource(String jndiDataSource) throws NamingException {
        Context context = new InitialContext();
        this.dataSource = (DataSource) context.lookup(jndiDataSource);
    }

    public WorkflowEngine getWorkflowEngine() {
        try {
            return new WorkflowEngineImpl(dataSource, idGenerator);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
